//
//  LHYPDFEditToolView.m
//  DrawLine
//
//  Created by lhy on 2021/3/2.
//

#import "LHYPDFEditToolView.h"
#import "LHYPDFEditMoreToolVC.h"

@interface LHYPDFEditToolView () <UIPopoverPresentationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *btnLastStep;
@property (weak, nonatomic) IBOutlet UIButton *btnNextStep;
@property (weak, nonatomic) IBOutlet UIButton *btnPen1;
@property (weak, nonatomic) IBOutlet UIButton *btnPen2;
@property (weak, nonatomic) IBOutlet UIButton *btnPen3;
@property (weak, nonatomic) IBOutlet UIButton *btnColorBlack;
@property (weak, nonatomic) IBOutlet UIButton *btnColorBlue;
@property (weak, nonatomic) IBOutlet UIButton *btnColorGreen;
@property (weak, nonatomic) IBOutlet UIButton *btnColorYellow;
@property (weak, nonatomic) IBOutlet UIButton *btnColorRed;
@property (weak, nonatomic) IBOutlet UIButton *btnColorPurple;


@end

@implementation LHYPDFEditToolView

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)show {
    UIWindow *window = [[UIApplication sharedApplication] windows].firstObject;
    [window addSubview:self];
}

- (void)hide {
    [self removeFromSuperview];
}

/// 上一步（点击撤回）
- (IBAction)actionBtnLastStep:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PDFDrawRevocationNotification" object:@(-1)];
}
/// 下一步（点击反撤回）
- (IBAction)actionBtnNextStep:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"PDFDrawRevocationNotification" object:@(1)];
}
/// 画笔种类(宽度)
- (IBAction)actionBtnPen:(UIButton *)sender {
    if (self.getDrawWidthBlock) {
        self.getDrawWidthBlock(sender.tag);
    }
}
/// 画笔颜色
- (IBAction)actionBtnColor:(UIButton *)sender {
    NSLog(@"%@", sender.backgroundColor);
    
    if (self.getDrawColorBlock) {
        self.getDrawColorBlock(sender.backgroundColor);
    }
}
/// 更多（签名/文本）
- (IBAction)actionBtnMoreTools:(UIButton *)sender {
    
    LHYPDFEditMoreToolVC *vc = [[LHYPDFEditMoreToolVC alloc] init];
    vc.popoverType = TypeImageTitle;
    vc.dataArr = [NSMutableArray arrayWithArray:@[@"文本", @"签名"]];
    vc.preferredContentSize = CGSizeMake(150, 100);
    vc.modalPresentationStyle = UIModalPresentationPopover;
    vc.popoverPresentationController.sourceView = self.btnMoreTools;
    vc.popoverPresentationController.delegate = self;
    //rect参数是以view的左上角为坐标原点（0，0）
    vc.popoverPresentationController.sourceRect = self.btnMoreTools.bounds; //指定箭头所指区域的矩形框范围（位置和尺寸），以view的左上角为坐标原点
    //    vc.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    vc.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionDown; //箭头方向
    vc.popoverPresentationController.delegate = self;
    [self.viewController presentViewController:vc animated:YES completion:nil];
}



#pragma mark - UIPopoverPresentationControllerDelegate

/// 在默认的情况下,UIPopoverPresentationController 会根据是否是 iphone 和 ipad 来选择弹出的样式,如果当前的设备是 iphone ,那么系统会选择 modal 样式,并弹出到全屏.如果我们需要改变这个默认的行为,则需要实现代理,在代理 - adaptivePresentationStyleForPresentationController: 这个方法中返回一个 UIModalPresentationNone 样式
/// @param controller controller
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController{
    return YES;   //点击蒙版popover NO不消失， 默认YES消失
}


@end
